package com.cljh.service.impl;

import cn.hutool.core.date.DateTime;
import com.cljh.bean.Validation;
import com.cljh.mapper.ValidationMapper;
import com.cljh.service.ValidationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ValidationServiceImpl implements ValidationService {
    @Autowired
    ValidationMapper validationMapper ;
    @Transactional
    @Override
    public void saveCode(String email, String code, Integer code1, DateTime offsetMinute) {
        Validation validation = new Validation();
        validation.setEmail(email);
        validation.setEmailCode(code);
        validation.setType(code1);
        validation.setTime(offsetMinute);
        //删除同邮箱的验证
        Validation validation1 = validationMapper.selectByEmail(email);
        if (validation1!=null){

            String email1 = validation1.getEmail();


            if (email.equals(email1)){
                validationMapper.delete(validation1.getId());
            }
        }

        validationMapper.save(validation);
    }

    @Override
    public Validation selectByEmail(String email) {
        return validationMapper.selectByEmail(email);
    }
}
